package com.lanyou.cook.entity.domain;

import java.util.Date;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.Version;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.beanutils.PropertyUtils;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.lanyou.cook.common.DefaultDateJsonSerializer;
import com.lanyou.cook.entity.domain.userManagement.User;
import com.lanyou.cook.entity.kettle.KettleDirectory;
import com.lanyou.cook.entitylistener.domain.FieldSystemEntityListenerForKettleObject;
import com.lanyou.cook.entitylistener.domain.FieldSystemEntityListenerForLog;
import com.lanyou.esb.entity.Ftp;
import com.lanyou.esb.entity.Http;

@Entity
@EntityListeners({ AuditingEntityListener.class,FieldSystemEntityListenerForLog.class, FieldSystemEntityListenerForKettleObject.class })
@Table(name = "T_COOK_BU_FIELD_SYSTEM")
public class FieldSystem {

	private String id;
	
	private String code;

	private int version;

	private String name;

	private String remark;

	private String creator;

	private String modifier;

	private Date createTime;

	private Date modifyTime;
	
	/*private String isEnable="1";*/

	private Field field;
	
	private Set<SystemInterface> systemInterfaces;
	
	private Set<Http> httpInterfaces;
	
	private Set<Http> visibleHttpInterfaces;
	
	private Set<Ftp> visibleFtpInterfaces;

	private Set<User> users;
	
	private KettleDirectory kettleDirectory;
	
	public final static Specification<FieldSystem> EmptySpecification = new Specification<FieldSystem>(){
		@Override
		public Predicate toPredicate(Root<FieldSystem> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
			return null;
		}
	};
	
	public static Specification<FieldSystem> idSpecification(String id){
		return new Specification<FieldSystem>(){
			@Override
			public Predicate toPredicate(Root<FieldSystem> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				query.where(cb.equal(root.get("id"), id));
				return null;
			}
		};
	}
	
	@Id
	@GeneratedValue(generator = "uuidGenerator")
	@GenericGenerator(name = "uuidGenerator", strategy = "uuid")
	@Column(name = "FIELD_SYSTEM_ID", length = 36)
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	@Version
	@JsonIgnore
	public int getVersion() {
		return version;
	}

	public void setVersion(int version) {
		this.version = version;
	}
	
	@Column(name = "SYSTEM_CODE", nullable = false, unique = true, length = 50)
	public String getCode() {
		return code;
	}
	
	public void setCode(String code) {
		this.code = code;
	}

	@Column(name = "SYSTEM_NAME", nullable = false, unique = true, length = 50)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(length = 100)
	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	@CreatedBy
	@Column(length = 50, updatable=false)
	public String getCreator() {
		return creator;
	}

	public void setCreator(String creator) {
		this.creator = creator;
	}

	@LastModifiedBy
	@Column(length = 50)
	public String getModifier() {
		return modifier;
	}

	public void setModifier(String modifier) {
		this.modifier = modifier;
	}

	@Column(name = "CREATED_DATE", updatable=false)
	@JsonSerialize(using = DefaultDateJsonSerializer.class)
	@CreatedDate
	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	
	@Column(name = "LAST_UPDATED_DATE")
	@JsonSerialize(using = DefaultDateJsonSerializer.class)
	@LastModifiedDate
	public Date getModifyTime() {
		return modifyTime;
	}

	public void setModifyTime(Date modifyTime) {
		this.modifyTime = modifyTime;
	}
	
	/*@Column(name = "IS_ENABLE", nullable = false, length = 2)
	public String getIsEnable() {
		return isEnable;
	}

	public void setIsEnable(String isEnable) {
		this.isEnable = isEnable;
	}*/

	@ManyToOne
	@JoinColumn(name = "FIELD_ID")
	public Field getField() {
		return field;
	}

	public void setField(Field field) {
		this.field = field;
	}

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "ownerFieldSystem")
	@JsonIgnore
	public Set<SystemInterface> getSystemInterfaces() {
		return systemInterfaces;
	}

	public void setSystemInterfaces(Set<SystemInterface> systemInterfaces) {
		this.systemInterfaces = systemInterfaces;
	}
	
	@OneToMany(fetch = FetchType.LAZY, mappedBy = "fieldSystem")
	@JsonIgnore
	public Set<Http> getHttpInterfaces() {
		return httpInterfaces;
	}

	public void setHttpInterfaces(Set<Http> httpInterfaces) {
		this.httpInterfaces = httpInterfaces;
	}
	
	@ManyToMany
	@JoinTable(name = "T_COOK_BU_USER_SYSTEM", 
		joinColumns = @JoinColumn(name = "FIELD_SYSTEM_ID", referencedColumnName = "FIELD_SYSTEM_ID") , 
			inverseJoinColumns = @JoinColumn(name = "USER_ID", referencedColumnName = "USER_ID") 
	)
	public Set<User> getUsers() {
		return users;
	}

	public void setUsers(Set<User> users) {
		this.users = users;
	}
	
	@OneToOne(cascade = CascadeType.ALL, orphanRemoval = true)
	@JoinColumn(name = "R_DIRECTORY_ID")
	@JsonIgnore
	public KettleDirectory getKettleDirectory() {
		return kettleDirectory;
	}

	public void setKettleDirectory(KettleDirectory kettleDirectory) {
		this.kettleDirectory = kettleDirectory;
	}

	@Transient
	public String getFieldName() {
		try {
			return (String) PropertyUtils.getProperty(field, "name");
		} catch (Exception e) {
			return null;
		}
	}
	
	@Transient
	public String getFieldId() {
		try {
			return (String) PropertyUtils.getProperty(field, "id");
		} catch (Exception e) {
			return null;
		}
	}
	
	// 用于Spoon
	@Transient
	public Long getKettleDirectoryId() {
		try {
			return (long) PropertyUtils.getProperty(kettleDirectory, "id");
		} catch (Exception e) {
			return null;
		}
	}

	@ManyToMany
	@JoinTable(name = "T_ESB_BU_IF_VISIBLITY", 
		joinColumns = @JoinColumn(name = "IF_VISIBLE_SYSTEM_ID", referencedColumnName = "FIELD_SYSTEM_ID") , 
			inverseJoinColumns = @JoinColumn(name = "IF_ID", referencedColumnName = "IF_HTTP_ID") 
	)
	public Set<Http> getVisibleHttpInterfaces() {
		return visibleHttpInterfaces;
	}

	public void setVisibleHttpInterfaces(Set<Http> visibleHttpInterfaces) {
		this.visibleHttpInterfaces = visibleHttpInterfaces;
	}

	@ManyToMany
	@JoinTable(name = "T_ESB_BU_IF_VISIBLITY", 
		joinColumns = @JoinColumn(name = "IF_VISIBLE_SYSTEM_ID", referencedColumnName = "FIELD_SYSTEM_ID") , 
			inverseJoinColumns = @JoinColumn(name = "IF_ID", referencedColumnName = "IF_FTP_ID") 
	)
	public Set<Ftp> getVisibleFtpInterfaces() {
		return visibleFtpInterfaces;
	}

	public void setVisibleFtpInterfaces(Set<Ftp> visibleFtpInterfaces) {
		this.visibleFtpInterfaces = visibleFtpInterfaces;
	}
	
	


}
